package cn.rokhdelar.ams.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysAuthority;
import cn.rokhdelar.ams.management.domain.query.AuthorityQuery;
import cn.rokhdelar.ams.management.domain.vo.AuthorityVO;
import cn.rokhdelar.ams.management.mapper.SysAuthorityMapper;
import cn.rokhdelar.ams.management.service.SysAuthorityService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 针对表【sys_authority(权限表)】的数据库操作Service实现
 * @author rokhdelar
 * @since 2024-10-02
 * @version v1.0.0
*/
@Service
public class SysAuthorityServiceImpl extends ServiceImpl<SysAuthorityMapper, SysAuthority>
    implements SysAuthorityService{

    @Override
    @Transactional
    public PageVO<AuthorityVO> getAuthorityListByPage(AuthorityQuery query) {
        Page<SysAuthority> page = query.toPageDefaultSortByCreateTime();

        Page<SysAuthority> authorityPage = lambdaQuery()
                .like(query.getName() !=null, SysAuthority::getName, query.getName())
                .eq(query.getStatus() !=null, SysAuthority::getStatus, query.getStatus())
                .like(query.getAuthorityKey() !=null, SysAuthority::getAuthorityKey, query.getAuthorityKey())
                .page(page);

        return PageVO.of(authorityPage, AuthorityVO.class);
    }

    @Override
    @Transactional
    public AuthorityVO getAuthorityById(String id) {
        SysAuthority authority = getById(id);
        return BeanUtil.copyProperties(authority, AuthorityVO.class);
    }
}
